package com.jt.tenant.dialog;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.activity.AboutSoftwareActivity;
import com.jt.tenant.activity.EquipInfoActivity;
import com.jt.tenant.activity.EquipSettingActivity;
import com.jt.tenant.activity.FenceListActivity;
import com.jt.tenant.activity.FillFreeWayInfoAcitity;
import com.jt.tenant.activity.FreewayIntroduceActivity;
import com.jt.tenant.activity.InsCheckSelectActivity;
import com.jt.tenant.activity.InsInfoActivity;
import com.jt.tenant.activity.InsSelectActivity;
import com.jt.tenant.activity.InsSupInfoActivity;
import com.jt.tenant.activity.LoginActivity;
import com.jt.tenant.activity.MainActivity;
import com.jt.tenant.activity.UpPictureActivity;
import com.jt.tenant.activity.WifiControlActivity;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.conn.SetInsCancelResult;
import com.jt.tenant.fragment.HomeFragment;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

/**
 * Created by Administrator on 2016/4/26.
 */
public class TipDialog extends JTDialog implements TextView.OnClickListener {

    private TextView tv_message;

    private TextView tv_cancel, tv_comfirm;

    private ImageView close;

    private Class mClass;

    private Context context;

    //0:普通提示，1：电子围栏删除，2：退出登录,3：保险订单提交确认提示，4：保险订单提交成功提示,5:取消保险,6:隐私保护，7：保单订单补充信息确认提示
    //8：WIFI开关、密码变更提示；9：软件更新；10：开通高速服务删除图片提示；11：开通高速服务提示
    private int type = 0;

    private SetInsCancelResult setInsCancelResult = new SetInsCancelResult(new AsyCallBack<SetInsCancelResult.Info>(){

        @Override
        public void onSuccess(String toast, int type, SetInsCancelResult.Info info) throws Exception {

            super.onSuccess(toast, type, info);

            if(info.msg.equals("000")){

                JTApplication.jtPreferences.saveInsFlag(info.flag);

                JTApplication.INSTANCE.finishAllActivity();

                context.startActivity(new Intent(context,MainActivity.class));

            }else {

                UtilToast.show(context,info.msg);

            }

        }

    });

    public TipDialog(Context context) {

        super(context);

        this.context = context;

        setContentView(R.layout.dialog_show);

        initView();

    }

    private void initView() {

        (tv_cancel = (TextView) findViewById(R.id.id_cancel)).setOnClickListener(this);

        (tv_comfirm = (TextView) findViewById(R.id.id_confirm)).setOnClickListener(this);

        (close = (ImageView)findViewById(R.id.iv_dialog_close)).setOnClickListener(this);

        tv_message = (TextView) findViewById(R.id.id_message);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.id_cancel:

                this.dismiss();

                break;

            case R.id.iv_dialog_close:

                this.dismiss();

                break;

            case R.id.id_confirm:

                switch (type) {

                    //普通提示，无操作
                    case 0:

                        this.dismiss();

                        break;

                    //删除电子围栏
                    case 1:

                        if (FenceListActivity.deleteFence != null) {

                            FenceListActivity.deleteFence.delete();

                        }

                        this.dismiss();

                        break;

                    //退出登录
                    case 2:

                        //uid设置为false
                        JTApplication.jtPreferences.saveHaveUid(false);
                        //uid清除
                        JTApplication.jtPreferences.saveUserId("");
                        //token清除
                        JTApplication.jtPreferences.saveToken("");

                        JTApplication.jtPreferences.saveHaveObdid(false);

                        JTApplication.jtPreferences.clear();

                        JTApplication.jtPreferences.saveIsGuide(true);

                        JTApplication.INSTANCE.finishAllActivity();

                        getContext().startActivity(new Intent(getContext(), LoginActivity.class));

                        JPushInterface.setAliasAndTags(context, "", null, new TagAliasCallback() {
                            @Override
                            public void gotResult(int i, String s, Set<String> set) {

                                Log.e("JPush注销", "Jpush status: " + i);//状态

                            }
                        });

                        this.dismiss();

                        break;

                    case 3:

                        InsSelectActivity.commitInfo.commit();

                        this.dismiss();

                        break;

                    case 4:

                        getContext().startActivity(new Intent(getContext(), InsCheckSelectActivity.class));

                        JTApplication.INSTANCE.finishActivity(InsInfoActivity.class,InsSelectActivity.class);

                        this.dismiss();

                        break;

                    case 5:

                        JTApplication.jtPreferences.saveInsFlag("0");

                        setInsCancelResult.execute(context);

                        this.dismiss();

                        break;

                    case 6:

                        EquipSettingActivity.refreshLinster.refresh();

                        this.dismiss();

                        break;

                    case 7:

                        InsSupInfoActivity.commitInfo.commit();

                        this.dismiss();

                        break;

                    case 8:

                        WifiControlActivity.refreshLinster.refresh();

                        this.dismiss();

                        break;

                    case 9:

                        if(HomeFragment.updateSoftware != null){

                            HomeFragment.updateSoftware.update();

                        }else if(AboutSoftwareActivity.updateSoftware != null) {

                            AboutSoftwareActivity.updateSoftware.update();

                        }

                        this.dismiss();

                        break;

                    case 10:

                        if(UpPictureActivity.deletePic != null){

                            UpPictureActivity.deletePic.delete();

                        }

                        this.dismiss();

                        break;

                    case 11:

                        JTApplication.INSTANCE.finishActivity(FreewayIntroduceActivity.class,FillFreeWayInfoAcitity.class,UpPictureActivity.class);

                        this.dismiss();

                        break;

                    case 100:

                        getContext().startActivity(new Intent(getContext(), EquipInfoActivity.class));

                        this.dismiss();

                        break;

                }

        }
    }

    public void setTip(String message) {

        try {

            tv_message.setText(message);

        } catch (Exception e) {

        }

    }

    public void setType(int type){

        this.type = type;

    }

}
